<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-24 4:33 下午
 *
 */

namespace app\admin\controller;

use app\common\model\tang\Merchant;
use app\common\controller\AdminBaseController;
use app\common\model\tang\SaleReportStatistical as SaleReportrModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class SaleReportStatistical extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new SaleReportrModel();
    }

    public function index()
    {
        if (IS_AJAX){

            return $this->model->getAdminPageData($this->param, 'id desc');
        }
        $merchant_list =\app\common\model\tang\Merchant::field('id,merchant_sn,name,company,username')->where('status',1)->select();
        //获取菜单列表
        $zubie = (new \app\common\model\tang\GroupConfig())->select();
        $this->assign(compact("merchant_list","zubie"));
        return $this->fetch();
    }
    public function noRead(){
        $id = $this->request->param('id');
        $result =  $this->model->find($id);
        $merchantlist = explode(',',$result['merchant_list']);
        $merhcant_info = (new Merchant())->whereIn('id',$merchantlist)->field('name,berth_number')->whereNotNull('identification')->select();
        $this->assign('list',$merhcant_info);
        session('no_read',$merhcant_info->toArray());
        return $this->fetch();
    }
    public function export(){
        $this->exports(session('no_read'));
    }

    private static $cellKey = array(
        'A', 'B'
    );
    public function exports($data)
    {
        $name = '未上报商户';
        $head = ['商户名称','铺位号'];
        $styleArray = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
//                'name' => '黑体',
                'bold' => false,
//                'size' => 10
            ]];
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet()->setTitle('未上报商户');
        //第一行
        $sheet->mergeCells('A1:B1', '');
        $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', '未上报商户');
        $sheet->getStyle(1)->applyFromArray($styleArray);
        $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(30);
        $sheet->getStyle(2)->applyFromArray($styleArray);
        //第二行
        for ($i = 0; $i < count($head); $i++) {
            $spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置水平居中
            $spreadsheet->getActiveSheet()->getColumnDimension(self::$cellKey[$i])->setWidth(20);
            $sheet->setCellValue(self::$cellKey[$i] . '2', $head[$i]);
            $spreadsheet->getActiveSheet()->getStyle(self::$cellKey[$i] . '2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//设置垂直居中
        }
        /*--------------开始从数据库提取信息插入Excel表中------------------*/
        $row =2;
        foreach ($data as $order){
            $clo = 1;
            foreach ($order as $val){
                $sheet->getStyle($row)->applyFromArray($styleArray);
                $spreadsheet->setActiveSheetIndex(0)->setCellValueByColumnAndRow($clo,$row,$val);
                $clo++;
            }
            $row++;
        }
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');

        $objWriter->save('php://output');
        //删除清空：
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }
}